package com.wzy.mylistviewadaptertest.Views;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.wzy.mylistviewadaptertest.CommonAdapter.SingleItemLVAdapter;
import com.wzy.mylistviewadaptertest.CommonAdapter.MultiItemLVAdapter;
import com.wzy.mylistviewadaptertest.CommonAdapter.MultiItemTypeSupport;
import com.wzy.mylistviewadaptertest.CommonAdapter.ViewHolder;
import com.wzy.mylistviewadaptertest.ItemBean.News;
import com.wzy.mylistviewadaptertest.R;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ListView mListView;
    private TextView mSingleItem, mMultiItem;
    private Context mContext = MainActivity.this;
    private ArrayList<News> mBeautyDatas;
    private ArrayList<News> mGentlemanDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBeautyDatas = new ArrayList<>();
        mGentlemanDatas = new ArrayList<>();
        mListView = (ListView) findViewById(R.id.list_view);
        mSingleItem = (TextView) findViewById(R.id.single_item);
        mMultiItem = (TextView) findViewById(R.id.multi_item);
        mSingleItem.setOnClickListener(this);
        mMultiItem.setOnClickListener(this);
        for (int i = 0; i < 10; i++) {
            News news = new News("美女" + i, "在" + i + "食堂里吃饭", "2016.05.26");
            mBeautyDatas.add(news);
        }

        for (int i = 0; i < 10; i++) {
            News news = new News("帅哥" + i, "在" + i + "图书馆看书", "2016.05.26");
            mGentlemanDatas.add(news);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.single_item:
                singleItemListView();
                break;

            case R.id.multi_item:
                multiItemListView();
                break;
        }
    }

    private void singleItemListView() {
        mListView.setAdapter(new SingleItemLVAdapter<News>(mContext, mBeautyDatas, R.layout.list_item_left) {
            @Override
            public void convert(ViewHolder holder, News item) {
                holder.setText(R.id.tv_title, item.getTitle());
                holder.setText(R.id.tv_describe, item.getDescribe());
                holder.setText(R.id.tv_time, item.getTime());
            }
        });
    }

    private void multiItemListView() {
        mListView.setAdapter(new MultiItemLVAdapter<News>(mContext, mGentlemanDatas, support) {
            @Override
            public void convert(ViewHolder holder, News item) {
                holder.setText(R.id.tv_title, item.getTitle());
                holder.setText(R.id.tv_describe, item.getDescribe());
                holder.setText(R.id.tv_time, item.getTime());
            }
        });
    }

    MultiItemTypeSupport support = new MultiItemTypeSupport<News>() {
        @Override
        public int getLayoutId(int position, News o) {
            if (position % 3 == 0) {
                return R.layout.list_item_left;
            }
            return R.layout.list_item_right;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position, News o) {
            if (position % 3 == 0) {
                return 0;
            }
            return 1;
        }
    };

}
